package com.wn.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wn.dto.AddCourseDTO;
import com.wn.pojo.Course;
import com.wn.mapper.CourseMapper;
import com.wn.pojo.CourseDescription;
import com.wn.service.ICourseDescriptionService;
import com.wn.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wn.utils.PageData;
import com.wn.vo.CoursePublishVo;
import com.wn.vo.CourseQueryVo;
import com.wn.vo.CourseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author yellow docker
 * @since 2023-06-20
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {

    @Autowired
    private ICourseDescriptionService courseDescriptionService;

    @Autowired
    private CourseMapper courseMapper;


    /**
     * 添加课程
     *
     * @param addCourseDTO 多表操作 (要不都成功 或者都失败 )
     *                     需要开启事务
     *                     事务开启有多中:
     *                     1、编码的方式
     *                     2、xml的配置方式
     *                     3、注解的方式
     * @Transactional 开启事务
     */
    @Override
    @Transactional
    public String saveCourse(AddCourseDTO addCourseDTO) {

        //步骤
        // 01  添加 course 表数据
        Course course = new Course();
        BeanUtils.copyProperties(addCourseDTO, course);


//        save(course);
        //提供了保存或者更新的操作
        saveOrUpdate(course);

        // 02  添加 course_desc  表数据
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setId(course.getId());
        courseDescription.setDescription(addCourseDTO.getDescription());
//        courseDescriptionService.save(courseDescription);

        courseDescriptionService.saveOrUpdate(courseDescription);

        return course.getId();
    }

    @Override
    public AddCourseDTO findAddCourseDTO(String courseId) {

        //01 创建
        AddCourseDTO addCourseDTO = new AddCourseDTO();

        //addCourseDTO 是2个表组成  course/course_desc

        //查询 course
        Course course = getById(courseId);
        //查询 course_desc
        CourseDescription courseDescription = courseDescriptionService.getById(courseId);

        //组装 addCourseDTO
        BeanUtils.copyProperties(course, addCourseDTO);
        addCourseDTO.setDescription(courseDescription.getDescription());

        return addCourseDTO;
    }

    @Override
    public CoursePublishVo findPublish(String courseId) {
        return courseMapper.findPublish(courseId);
    }

    @Override
    public boolean publish(String courseId) {
        LambdaUpdateWrapper<Course> lambdaUpdate = Wrappers.lambdaUpdate(Course.class);

        lambdaUpdate.eq(Course::getId, courseId);
        lambdaUpdate.set(Course::getStatus, "Normal");
        boolean update = update(lambdaUpdate);
        return update;
    }

    /**
     * 分页的步骤:
     * 1、开启分页插件
     * 2、创建ipage 对象
     * 3、调用方法
     * 4、封装返回结果
     */
    @Override
    public PageData findPageCourse(Integer currentPage, Integer pageSize) {

        IPage<Course> p = new Page<>(currentPage, pageSize);
        IPage<Course> page = page(p, null);

        PageData pageData = new PageData();
        pageData.setTotal(page.getTotal());
        pageData.setData(page.getRecords());

        return pageData;
    }

    @Override
    public List<CourseVo> findHotPageCourse() {

        //分页查询
        IPage<Course> p = new Page<>(1, 4);

        //通过  发布的时间进行排序
        LambdaQueryWrapper<Course> courseLambdaQueryWrapper = Wrappers.lambdaQuery(Course.class);

        //查询发布的课程
        courseLambdaQueryWrapper.eq(Course::getStatus, "Normal");
        //通过发布的时间进行 排序
        courseLambdaQueryWrapper.orderByDesc(Course::getUpdateTime);

        //调用分页的方法
        IPage<Course> courseIPage = page(p, courseLambdaQueryWrapper);

        List<Course> courses = courseIPage.getRecords();

        //把  Course 转成  vo
        List<CourseVo> courseVos = BeanUtil.copyToList(courses, CourseVo.class);

        return courseVos;
    }

    @Override
    public PageData findAllPageCourse(Integer currentPage, Integer pageSize) {

        IPage<CourseQueryVo> p = new Page<>(currentPage,pageSize);

        IPage<CourseVo> page = courseMapper.findAllPageCourse(p);

        PageData pageData = new PageData();
        pageData.setTotal(page.getTotal());
        pageData.setData(page.getRecords());

        return pageData;
    }
}
